<template>
  <div class="banner-setting">
    <!-- 操控区 
     @click="visible = true"
    -->
    <div class="handler-box">
      <el-button
        type="primary"
        icon="el-icon-plus"
        size="small"
        @click="openDialog('')"
      >
        新增
      </el-button>
      <el-button
        type="danger"
        icon="el-icon-delete"
        size="small"
        @click="removeAll"
      >
        批量删除
      </el-button>
    </div>
    <!-- 表格区 -->
    <el-table
      :data="tableData"
      stripe
      style="width: 100%"
      @selection-change="changeSelects"
    >
      <el-table-column
        type="selection"
        min-width="5%"
        align="center"
      ></el-table-column>
      <el-table-column prop="configName" label="商品名称" align="center">
      </el-table-column>

      <el-table-column prop="redirectUrl" label="跳转链接" align="center">
      </el-table-column>
      <el-table-column prop="configRank" label="排序值" align="center">
      </el-table-column>
      <el-table-column prop="goodsId" label="商品编号" align="center">
      </el-table-column>
      <el-table-column
        prop="createTime"
        label="添加时间"
        align="center"
        :formatter="formatterTime"
      >
      </el-table-column>

      <el-table-column label="操作" align="center">
        <template #default="{ row }">
          <el-link type="success" @click="openDialog(row.configId, row)"
            >修改</el-link
          >
          <el-popconfirm
            title="确定要删除本条数据吗？"
            @confirm="remove([row.configId])"
          >
            <el-link type="primary" slot="reference">删除</el-link>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页区 -->
    <el-pagination
      background
      align="center"
      layout="prev, pager, next,jumper"
      :total="total"
      :hide-on-single-page="true"
      :page-size="pageSize"
      :current-page="page"
      @current-change="paginationChange"
      @prev-click="paginationChange"
      @next-click="paginationChange"
    >
    </el-pagination>
    <!-- 新增/修改 -->
    <el-dialog
      title="新增任务窗口"
      :visible="visible"
      :before-close="closeDialog"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
    >
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        label-width="80px"
      >
        <el-form-item label="商品名称" prop="configName">
          <el-input v-model.trim="ruleForm.configName"></el-input>
        </el-form-item>
        <el-form-item label="跳转连接" prop="redirectUrl">
          <el-input v-model.trim="ruleForm.redirectUrl"></el-input>
        </el-form-item>
        <el-form-item label="商品序号" prop="goodsId">
          <el-input-number
            v-model.trim="ruleForm.goodsId"
            controls-position="right"
            :min="1"
            :max="100000"
          ></el-input-number>
        </el-form-item>
        <el-form-item label="排序值" prop="configRank">
          <el-input-number
            v-model.trim="ruleForm.configRank"
            controls-position="right"
            :min="1"
            :max="200"
          ></el-input-number>
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button type="primary" @click="submit()">确认提交</el-button>
        <el-button type="info" @click="closeDialog">取消</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { formatTime } from "@/assets/utils";
export default {
  name: "SellHot",
  props: ["num"],
  data() {
    return {
      configId: "",
      configType: this.num,
      tableData: [],
      pageSize: 10,
      total: 0,
      page: 1,
      visible: false,
      selects: [],
      ruleForm: {
        configName: "",
        goodsId: "",
        redirectUrl: "",
        configRank: "",
      },
      rules: {
        configName: [
          { required: true, message: "请输入活动名称", trigger: "blur" },
        ],
        configName: [
          { required: true, message: "请输入跳转网址", trigger: "blur" },
        ],
        configRank: [
          { required: true, message: "必须设置商品序号！", trigger: "blur" },
          {
            type: "number",
            min: 1,
            max: 200,
            message: "排序值无效",
            trigger: "blur",
          },
        ],
        goodsId: [
          { required: true, message: "必须设置排序值！", trigger: "blur" },
          {
            type: "number",
            min: 1,
            max: 2000000,
            message: "排序值无效",
            trigger: "blur",
          },
        ],
      },
    };
  },
  methods: {
    async query() {
      try {
        let { resultCode, data } = await this.$api.sellHot.indexConfigs(
          this.page,
          this.pageSize,
          this.configType
        );
        if (+resultCode !== 200) {
          this.tableData = [];
          return;
        }

        let { totalCount, list } = data;
        this.tableData = list;
        console.log(this.tableData);
        this.total = totalCount;
      } catch (_) {
        this.tableData = [];
        this.total = 0;
      }
    },
    //打开新增/修改
    openDialog(configId = "", row) {
      this.visible = true;
      this.configId = configId;

      if (configId) {
        console.log("111");
        let { configName, goodsId, redirectUrl, configRank } = row;
        this.ruleForm = {
          configName,
          goodsId,
          redirectUrl,
          configRank,
        };
      } else {
        console.log("222");
        this.configId = "";
        this.ruleForm.configName = "";
        this.ruleForm.goodsId = "";
        this.ruleForm.redirectUrl = "";
        this.ruleForm.configRank = "";
      }
    },
    //分页
    paginationChange(val) {
      this.page = +val;
      this.query();
    },
    //关闭新增
    closeDialog() {
      this.visible = false;
      this.configId = "";

      this.$refs["ruleForm"].resetFields();
    },
    //确认提交
    async submit() {
      try {
        await this.$refs["ruleForm"].validate();
        let sendFunc = this.$api.sellHot.indexConfigsInsert,
          configId = this.configId, //块级优化，不用每次都get,可以把this.去掉
          { configName, goodsId, redirectUrl, configRank } = this.ruleForm,
          /*  data = { ...this.ruleForm }; */
          data = {
            configName,
            configType: this.configType,
            goodsId,
            redirectUrl,
            configRank,
          };

        if (configId) {
          //修改
          sendFunc = this.$api.sellHot.indexConfigsUpdate;
          data.configId = this.configId;
        }
        console.log("111:", data);
        let { resultCode } = await sendFunc(data);
        if (+resultCode !== 200) {
          this.$message.error("当前操作失败，请稍后再试");
          return;
        }
        this.$message.success("恭喜您，当前操作成功~");
        if (this.configId === "") this.page = 1;
        this.query();
        this.closeDialog();
      } catch (_) {}
    },
    //删除
    async remove(ids) {
      let { resultCode } = await this.$api.sellHot.indexConfigsRemove(ids);
      if (+resultCode !== 200) {
        this.$message.error("删除失败，请稍后再试~~~");
        return;
      }
      this.$message.success("删除成功");
      if (this.page !== 1 && this.tableData.length === 1) {
        this.page--;
      }
      this.query();
    },
    //把勾选的放给selects
    changeSelects(val) {
      this.selects = val.map((item) => item.configId);
    },
    //批量删除

    async removeAll() {
      let selects = this.selects;
      if (selects.length == 0) {
        this.$message.error("请至少选择一项进行删除");
        return;
      }
      try {
        this.$confirm("此操作将永久删除选中项, 是否继续?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        });
        this.remove(selects);
      } catch (_) {}
    },
    //修改时间
    formatterTime(row) {
      return formatTime(row.createTime, "{1}-{2} {3}:{4}");
    },
  },
  created() {
    this.query();
  },
};
</script>

<style lang="less" scoped>
.banner-setting {
  position: relative;
  box-sizing: border-box;
  height: 100%;
  padding: 40px 0;
  /deep/.el-link--inner {
    margin-right: 10px;
  }

  /* .handler-box {
    position: absolute;
    top: 0;
    left: 0;
    z-index: 999;
    display: flex;
    align-items: center;
    box-sizing: border-box;
    width: 100%;
    height: 40px;
  } */

  /* .el-pagination {
    position: absolute;
    bottom: 0;
    right: 0;
    z-index: 999;
    display: flex;
    align-items: center;
    height: 40px;
  }

  .el-link {
    margin-right: 10px;
  }

  /deep/.el-dialog__body {
    padding: 10px 20px;
  }

  /deep/.el-upload {
    display: block;
  }

  .avatar-uploader-icon,
  .avatar-uploader-img {
    display: block;
    box-sizing: border-box;
    width: 120px;
    height: 120px;
    line-height: 120px;
    font-size: 24px;
    color: #8c939d;
    border: 1px dashed #d9d9d9;
    cursor: pointer;
  }
  /deep/.el-table__empty-text {
    line-height: normal;
    .el-link {
      display: inline-block;
      vertical-align: top;
      margin-right: 0;
    }
  }
  /deep/.el-form-item__label {
    display: inline;
  }

  /deep/.el-dialog__body {
    padding: 10px 20px;
  } */
}
</style>